package controller;

import Dao.UserApplicationDao;
import Response.Follow;
import Util.FileUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import modal.User;
import modal.UserApplication;
import modal.UserFollow;
import modal.UserVideo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import service.ActivityService;
import service.AdminManageService;
import service.PersonManageService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
public class Test {

    @Autowired
    PersonManageService personManageService;

    @Autowired
    AdminManageService adminManageService;

    @Autowired
    UserApplicationDao userApplicationDao;

    @Autowired
    ActivityService activityService;

    @RequestMapping(value = "/test1.do")
    @ResponseBody
    public String Test(HttpSession httpSession){
        User user = (User)httpSession.getAttribute("user");
        user.setPhone("18717893270");
        personManageService.updateInfo(user);
        return JSON.toJSONString(user, SerializerFeature.WriteMapNullValue);
    }


    // 测试获取关注列表  result:success
    @RequestMapping(value = "/test2.do")
    @ResponseBody
    public String test2(@RequestParam(value = "username") String username){
        List<Follow> list = personManageService.getfollows(username);
        System.out.println(JSON.toJSONString(list,SerializerFeature.WriteMapNullValue));
        return JSON.toJSONString(list,SerializerFeature.WriteMapNullValue);
    }

    //测试取消关注  result:success
    @RequestMapping(value ="/test3.do")
    @ResponseBody
    public String test3(UserFollow user)
    {
        String username = user.getUsername();
        String roomid= user.getRoomid();
        return personManageService.deletefollow(username,roomid);
    }


    //测试提交申请(包括判断是否重复)  result:sucess
    @RequestMapping(value = "/test4.do")
    @ResponseBody
    public String test4(UserApplication application)
    {
        return personManageService.submitApplication(application);
    }


    //测试审批申请 result:success
    @RequestMapping(value = "/test5.do")
    @ResponseBody
    public String test5()
    {
        StringBuilder result = new StringBuilder();
        result.append(JSON.toJSONString(adminManageService.getAllApplication(),SerializerFeature.WriteMapNullValue));
        UserApplication application1 = userApplicationDao.getOneApplication("asd");
        UserApplication application2 = userApplicationDao.getOneApplication("asd2");
        result.append(adminManageService.allowApplication(application1));
        result.append(adminManageService.refuseApplication(application2));
        return result.toString();
    }

    //关注加分 result:success
    @RequestMapping(value = "/test6.do")
    @ResponseBody
    public String test6()
    {
        return (activityService.followEvent("tanlian","6666"));
    }

    //取消订阅  result:success
    @RequestMapping(value = "/test7.do")
    @ResponseBody
    public String test7()
    {
        return activityService.unfollowEvent("tanlian","6666");
    }


    //测试上传文件功能
    @RequestMapping(value = "/test8.do" )
    @ResponseBody
    public String test8(@RequestParam("file") MultipartFile file,
                        HttpServletRequest request,
                        HttpServletResponse response)
    {
        System.out.println(file);
        FileUtil fileUtil  = new FileUtil(file,request);
        return fileUtil.uploadVideo();
    }

    //上传图像功能
    @RequestMapping(value = "/110")
    public String test9()
    {

        return "login";
    }

}
